package com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.repository.Impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.entity.EmergencyReport;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.param.EmergencyReportParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.repository.EmergencyReportRepository;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/30
 * @Description: 灾情急报
 */
@Repository
public class EmergencyReportRepositoryImpl implements EmergencyReportRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_disaster_emergency_report"; // 表名
    private static final String TABLE_ID = "xder_id"; // 表id字段名

    @Override
    public Map<String, Object> getPage(EmergencyReportParam queParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from "+TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        //地震事件id
        if (!PlatformObjectUtils.isEmpty(queParams.getXsemId())) {
            whereSql.append(" and xsem_id = '" + queParams.getXsemId() + "'");
        }
        //灾情点编号
        if (!PlatformObjectUtils.isEmpty(queParams.getDisasterCode())) {
            whereSql.append(" and disaster_code like '%" + queParams.getDisasterCode() + "%'");
        }
        //灾情点名称
        if (!PlatformObjectUtils.isEmpty(queParams.getSiteName())) {
            whereSql.append(" and site_name like '%" + queParams.getSiteName() + "%'");
        }
        //调查员
        if (!PlatformObjectUtils.isEmpty(queParams.getInvestigator())) {
            whereSql.append(" and investigator like '%" + queParams.getInvestigator() + "%'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(EmergencyReport.class)).size();
        List<EmergencyReport> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(EmergencyReport.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public List<EmergencyReport> queryByParam(EmergencyReportParam queParams) {
        StringBuilder sql = new StringBuilder("select * from "+TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        //地震事件id
        if (!PlatformObjectUtils.isEmpty(queParams.getXsemId())) {
            whereSql.append(" and xsem_id = '" + queParams.getXsemId() + "'");
        }
        //灾情点编号
        if (!PlatformObjectUtils.isEmpty(queParams.getDisasterCode())) {
            whereSql.append(" and disaster_code like '%" + queParams.getDisasterCode() + "%'");
        }
        //灾情点名称
        if (!PlatformObjectUtils.isEmpty(queParams.getSiteName())) {
            whereSql.append(" and site_name like '%" + queParams.getSiteName() + "%'");
        }
        //调查员
        if (!PlatformObjectUtils.isEmpty(queParams.getInvestigator())) {
            whereSql.append(" and investigator like '%" + queParams.getInvestigator() + "%'");
        }
        sql.append(whereSql).append(orderSql);
        List<EmergencyReport> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(EmergencyReport.class));
        return list;
    }

    @Override
    public EmergencyReport findById(String id) {
        String sql = "select * from " + TABLE_NAME + " where " + TABLE_ID + " = '" + id + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EmergencyReport.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    //新增灾情急报
    @Override
    public void save(EmergencyReport param) {
        String sql = "insert into " + TABLE_NAME +
                " (xder_id,province,city,county,detail_address,longitude,latitude," +
                "disaster_code,site_name,death,people_trapped," +
                "building_collapse,crumble,landslip,congestion_disruption,fire,hazardous_material_leakage,other," +
                "detail_desc,enclosure_photo,enclosure_video,enclosure_audio,enclosure_album,create_user,create_time,update_user,update_time," +
                "submit,del_flag,type,investigator,xsem_id,geom)" +
                " values(:xderId,:province,:city,:county,:detailAddress,:longitude,:latitude," +
                ":disasterCode,:siteName,:death,:peopleTrapped,:buildingCollapse," +
                ":crumble,:landslip,:congestionDisruption,:fire,:hazardousMaterialLeakage,:other," +
                ":detailDesc,:enclosurePhoto,:enclosureVideo,:enclosureAudio,:enclosureAlbum,:createUser,:createTime,:updateUser,:updateTime,:submit," +
                ":delFlag,:type,:investigator,:xsemId,ST_GeometryFromText(:geom,4490))";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void submit(String id) {
        String sql="update "+TABLE_NAME+" set submit= '1' where "+TABLE_ID+" = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateXsemId(String xsemId, String convertedXsemId) {
        String sql = "update "+TABLE_NAME+" set xsem_id = '"+xsemId+"'  where xsem_id = '"+convertedXsemId+"'";
        jdbcTemplate.update(sql);
    }

    //修改灾情急报
    @Override
    public void update(EmergencyReport param) {
        String sql = "update "+TABLE_NAME+" set province =:province,city =:city,county =:county," +
                " detail_address = :detailAddress,longitude=:longitude,latitude=:latitude," +
                " disaster_code = :disasterCode, site_name=:siteName,death=:death,people_trapped=:peopleTrapped," +
                " building_collapse=:buildingCollapse,crumble=:crumble," +
                " landslip=:landslip,congestion_disruption=:congestionDisruption,fire=:fire," +
                " hazardous_material_leakage=:hazardousMaterialLeakage,other=:other,detail_desc=:detailDesc," +
                " enclosure_photo=:enclosurePhoto,enclosure_video=:enclosureVideo,enclosure_audio=:enclosureAudio,enclosure_album=:enclosureAlbum,type=:type," +
                " geom=ST_GeometryFromText(:geom,4490) where xder_id = '"+param.getXderId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void batchDelete(List<String> id) {
        String sql="update "+TABLE_NAME+" set del_flag= '1' where "+TABLE_ID+"  in  ('" + String.join("','", id) + "')";
        jdbcTemplate.update(sql);
    }

    @Override
    public String getCode(String code) {
        String sql="select max(disaster_code) from "+TABLE_NAME+" where del_flag = '0' and disaster_code like '%"+code+"%'";
        String maxNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxNum;
    }
}
